package com.cntcdt.api.db.dao;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.cntcdt.api.db.dao.base.GroupUserDaoBase;
import com.cntcdt.api.db.model.GroupUserModel;
import com.cntcdt.api.utils.LogUtils;
import com.cntcdt.api.utils.ReflectUtils;

/**
 * 这是cdt_group_user的Dao实现类
 */
public class GroupUserDao extends GroupUserDaoBase   {
    /**
     * GroupUserDao对象
     */
    private static GroupUserDao instance = new GroupUserDao();

    /**
     * 默认构造方法
     */
    private GroupUserDao() {
    }

    /**
     * 获得对象实例
     */
    public static GroupUserDao getInstance() { 
         return instance;
    } 
    
    public GroupUserModel findByUsernameAndPassword(String username,String password) {
        String sql = "SELECT * FROM " + TABLE + " WHERE username = ? and password = ? and opStatus = 0";
       Object[] paramObjs = {username,password};
       List<GroupUserModel > modelList = this.queryModelList(sql, paramObjs);
       if(modelList != null && modelList.size() > 0){ 
             return  modelList.get(0);
       } 
       return null;
   }
    
    public List<GroupUserModel> findByGroupId(int id) {
        String sql = "SELECT * FROM " + TABLE + " WHERE groupId = ? and opStatus = 0 ORDER BY level ASC";
       Object[] paramObjs = {id};
       List<GroupUserModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   } 
    
    public GroupUserModel findByUsername(String username) {
        String sql = "SELECT * FROM " + TABLE + " WHERE username = ? and opStatus = 0";
       Object[] paramObjs = {username};
       List<GroupUserModel > modelList = this.queryModelList(sql, paramObjs);
       if(modelList != null && modelList.size() > 0){ 
             return  modelList.get(0);
       } 
       return null;
   }
    
    
    /**
     * 更新操作，自动根据userModel中已经设置的字段进行更新
     * 必须要设置userModel.id属性 和 其他任意一个属性
     * 
     * 返回更新结果
     */
    public boolean updateAny(Object model){
    	StringBuffer sql = new StringBuffer("update  "+TABLE+" set ");
		List<Object> sqlArgs = new ArrayList<Object>();
		Field[] superfields = model.getClass().getSuperclass().getDeclaredFields();
//		Field[] fields = userModel.getClass().getDeclaredFields();
		for(Field field : superfields){
			if(field.getName().equals("serialVersionUID") || field.getName().equals("id")){
				continue;
			}
			if(ReflectUtils.getter(model, field.getName())!=null){
				sql.append(" "+field.getName()+"=? ,");
				sqlArgs.add(ReflectUtils.getter(model, field.getName()));
			} 
		}
		
		String sqlstr = sql.toString();
		sqlstr = sqlstr.substring(0, sqlstr.length()-1);
		sqlstr = sqlstr + " where id=?";
		sqlArgs.add(ReflectUtils.getter(model,"id"));
		LogUtils.log.info(sqlstr);
		LogUtils.log.info(sqlArgs.toString());
		
		Object[]  paramObj = sqlArgs.toArray();
        try {
             return writeDBEngine.executeUpdate(sqlstr, paramObj)>0;
        } catch(Exception e) {
           e.printStackTrace();
        }
        return false;
    }
    
    public List<GroupUserModel> findBySortTime(long sortTime) {
    	sortTime = sortTime*20;
        String sql = "SELECT * FROM " + TABLE + " WHERE opStatus = 0 and groupId = 87 order by level,sortTime desc limit ?,20";
       Object[] paramObjs = {sortTime};
       List<GroupUserModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   }
    
    public List<GroupUserModel> findByGroupId(int id,long sortTime) {
    	sortTime = sortTime*20;
        String sql = "SELECT * FROM " + TABLE + " WHERE groupId = ? and opStatus = 0 order by sortTime desc limit ?,20";
       Object[] paramObjs = {id,sortTime};
       List<GroupUserModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   } 
    
    public List<GroupUserModel> findBySearchContent(String searchContent,long sortTime) {
        String sql = "SELECT * FROM " + TABLE + " WHERE name LIKE ? and opStatus = 0 and sortTime < ? order by sortTime desc limit 20";
       Object[] paramObjs = {searchContent,sortTime};
       List<GroupUserModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   }
    
    public List<GroupUserModel> findByLargerThanLevel(int level,long sortTime) {
        String sql = "SELECT * FROM " + TABLE + " WHERE level < ? and opStatus = 0 and sortTime < ? order by sortTime desc limit 100";
       Object[] paramObjs = {level,sortTime};
       List<GroupUserModel > modelList = this.queryModelList(sql, paramObjs);
       return modelList;
   }
}
